package com.ruoyi.web.controller.applet;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.model.param.UserRegParam;
import com.ruoyi.system.model.result.LoginUserResult;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITudaAppletUserService;
import com.ruoyi.system.service.ITudaVerificationCodeService;
import com.ruoyi.system.service.IWxDecodeService;
import com.ruoyi.system.service.impl.WxDecodeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/applet/user")
@Api(tags = "小程序用户相关")
public class UserLoginController {

    @Resource
    private ITudaAppletUserService userService;
    @Resource
    private ITudaVerificationCodeService verificationCodeService;
    @Resource
    private IWxDecodeService wxDecodeService;
    @Resource
    private ISysUserService sysUserService;

    @GetMapping("/sendSms.do")
    @ApiOperation("请求发送验证码")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "验证码类型 1登录/注册，2绑定手机号，3修改密码，4 更换手机号", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query")})
    public R askSendSms(Integer type, String phone) {
        return verificationCodeService.askSendSms(type, phone);
    }

    @GetMapping("/askCheckSms.do")
    @ApiOperation("请求验证验证码")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "验证码类型 1登录/注册，2绑定手机号，3修改密码", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", paramType = "query")})
    public R askCheckSms(Integer type, String phone, String code) {
        return verificationCodeService.askCheckSms(type, phone, code);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public R<LoginUserResult> login(@RequestBody UserRegParam params) {
        return userService.selectTudaAppletUserByPhone(params);
    }

    @ApiOperation(value = "微信小程序授权登录")
    @GetMapping(value = "/weChat/miniDecode.do")
    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "code")})
    public R wxMiniDecode(String code) {
        return wxDecodeService.wxMiniDecode(code);
    }

    @ApiOperation(value = "微信小程序授权获取手机号")
    @GetMapping(value = "/weChat/wxMiniDecodePhone.do")
    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "code"), @ApiImplicitParam(name = "userId", value = "userId")})
    public R<String> wxMiniDecodePhone(String code, String userId) {
        return wxDecodeService.getWxMiniDecodePhone(code, userId);
    }

    @ApiOperation(value = "获取jsApi_ticket")
    @GetMapping(value = "/weChat/getJsApiTicket.do")
    @ApiImplicitParams({@ApiImplicitParam(name = "url", value = "url")})
    public R getJsApiTicket(String url){
        return wxDecodeService.getJsApiTicket(url);
    }


    @GetMapping("/selectDepUser/{deptId}")
    public List<SysUser> selectDepUser(@PathVariable("deptId") Long deptId){
        return sysUserService.queryUserByDepId(deptId);
    }

}
